package ibmetp.mis.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import ibmetp.mis.common.JdbcUtil;


public class UserToRoleDao {
	protected Connection conn;
	public void addRoleUser(String roleIds,String userIds)
	{
		String[] roleids=JdbcUtil.string2Array(roleIds);
		String[] userids=JdbcUtil.string2Array(userIds);
		for(int i=0;i<roleids.length;i++)
		{
			for(int j=0;j<userids.length;j++)
			{
				String sql="insert into BAS_T_USER_ROLE(USER_ROLE_ID,USER_ID,ROLE_ID,CREATE_DATE) values(USER_ROLE_SEQ.NEXTVAL,?,?,sysdate)";
				conn=DBUtil.getConnection();
				PreparedStatement ps;
				
				try {
					ps=conn.prepareStatement(sql);
					ps.setLong(1, Long.valueOf(userids[j]));
					ps.setLong(2, Long.valueOf(roleids[i]));
				    ResultSet rs=ps.executeQuery();
				    ps.close();
				    conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	public void removeUserRole(String userIds, String roleIds)
	{
		String[] roleids=JdbcUtil.string2Array(roleIds);
		String[] userids=JdbcUtil.string2Array(userIds);
		for(int i=0;i<roleids.length;i++)
		{
			for(int j=0;j<userids.length;j++)
			{
				String sql="delete from BAS_T_USER_ROLE where USER_ID=? and ROLE_ID=?";
				conn=DBUtil.getConnection();
				PreparedStatement ps;
				
				try {
					ps=conn.prepareStatement(sql);
					ps.setLong(1, Long.valueOf(userids[j]));
					ps.setLong(2, Long.valueOf(roleids[i]));
				    ResultSet rs=ps.executeQuery();
				    ps.close();
				    conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
